<template>
  <div style="height:100%">

    <!-- 搜索区域 -->
    <div>
      <el-form :inline="true" :model="formInline" size='mini'>
        <el-form-item label="设备ID">
          <el-input v-model.trim="formInline.devId" placeholder="设备ID"></el-input>
        </el-form-item>
        <el-form-item label="机车号">
          <el-input v-model.trim="formInline.en" placeholder="机车号"></el-input>
        </el-form-item>
        <el-form-item label="测试日期">
          <el-date-picker
                  v-model="formInline.testDate"
                  type="date"
                  placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchList" :loading="btnLoading">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 列表展示区域 -->
    <div style="height:85%">
      <el-table :data="tableData" :border="true" style="width: 100%" v-loading="tableLoading" height="100%" size='mini'>
        <el-table-column prop="devId" label="设备ID"></el-table-column>
        <el-table-column prop="en" label="机车号"></el-table-column>

        <el-table-column prop="lineName" label="线路名称"></el-table-column>

        <el-table-column prop="tn" label="车次号"></el-table-column>

        <el-table-column prop="networkState" label="网络状态"></el-table-column>

        <el-table-column prop="updateTime" label="更新时间" :formatter="showTime">
        </el-table-column>
        <el-table-column prop="onlineState" label="在线状态">
          <!-- :formatter="showOnlineState" -->
          <template slot-scope="scope">
            <el-image style="width: 25px; height: 25px" :src="require('@/assets/online.png')"
              v-if="scope.row.onlineState"></el-image>
            <el-image style="width: 25px; height: 25px" :src="require('@/assets/offline_fill.png')" v-else></el-image>
            <!-- <span
              style="color:green;font-weight:bold;"
              v-if="scope.row.onlineState"
            >在线</span>
            <span
              style="color:orange;"
              v-else
            >离线</span> -->
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <template v-if="from">
              <el-button @click.native.prevent="confirm(scope.$index, tableData)" type="text" size="small">
                确认
              </el-button>
            </template>
            <template v-else>
              <el-button @click.native.prevent="showDetail(scope.$index, tableData)" type="text" size="small">
                详情
              </el-button>
            </template>

          </template>
        </el-table-column>

      </el-table>
      <!-- 分页 -->
      <!-- 
        @size-change //  pageSize 改变时会触发 每页条数
        @current-change //  currentPage 改变时会触发 当前页
        :current-page //  默认false
        background//  是否为分页按钮添加背景色
        :page-sizes // 每页显示个数选择器的选项设置 这是下拉框可以选择的，每选择一行，要展示多少内容 类似：[10, 20, 30, 40, 50, 100]
        page-sizes=显示当前行的条数
        layout // 组件布局，子组件名用逗号分隔
       :total // 总条目数,一般从展示列表的总数获取
     -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        background :page-sizes="[10, 20, 30, 50]" :page-size="pagesize" layout="total, sizes,  prev, pager, next, jumper"
        :total="totalCount">
      </el-pagination>

    </div>

    <!-- 详情展示区域 -->
    <dev-detail ref="detail"></dev-detail>
  </div>
</template>

<script>
import DevDetail from '@/views/dev/DevDetail';
import { getAction } from '@/api/manage';
import { getNowDate } from '@/util/util';
export default {
  components: {
    DevDetail,
  },
  props: ['from'],
  data() {
    return {
      formInline: {
        devId: '',
        en: '',
      },
      btnLoading: false,
      tableData: [],
      tableLoading: false,

      currentPage: 1, // 初始页
      pagesize: 10, // 初始每页的数据
      totalCount: 0, //总数
    };
  },
  created() {
    this.onSubmit();
  },
  methods: {
    showTime(row, column) {
      let tmp = row[column.property];
      return getNowDate(tmp);
    },
    showOnlineState(row, column) {
      let tmp = row[column.property];
      if (tmp) {
        return '<span color="green">在线</span>';
      } else {
        return '<span color="green">离线</span>';
      }
    },
    /**
     * 确认
     */
    confirm(index, rows) {
      console.log('from', this.from);
      let row = rows[index];
      let devId = row.devId;
      let en = row.en;
      this.$emit('confirm', { devId, en });
    },
    showDetail(index, rows) {
      //详情信息
      let row = rows[index];
      let devId = row.devId;
      this.$refs.detail.show(devId);
    },
    /**
     * 列表查询
     */
    searchList() {
      this.currentPage = 1;
      this.onSubmit();
    },
    onSubmit() {
      this.btnLoading = true;
      this.tableLoading = true;
      getAction('/device/queryDevBasicStateListContainPage', {
        devId: this.formInline.devId,
        en: this.formInline.en,
        currentPage: this.currentPage,
        pageSize: this.pagesize,
      })
        .then((res) => {
          if (res.code == 200) {
            this.tableData = res.data.records;
            this.totalCount = res.data.total;
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((err) => {
          this.$message.error(err.message);
        })
        .finally(() => {
          this.btnLoading = false;
          this.tableLoading = false;
        });
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      this.pagesize = size;
      console.log(this.pagesize); //每页下拉显示数据
      this.onSubmit();
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
      console.log(this.currentPage); //点击第几页
      this.onSubmit();
    },
  },
};
</script>

<style lang="scss">
.demo-ruleForm label {
  color: black;
}
</style>